package com.starblink.wishlist.detailsadd.ui

import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.basic.route.RoutePage
import com.starblink.android.basic.base.activity.BaseTVMActivity
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.animators.FadeInAnimator
import com.starblink.android.basic.const.NavParamsKeyConst
import com.starblink.android.basic.ext.*
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.basic.style.databinding.EmptyViewWishFilterBinding
import com.starblink.basic.style.title.OnTitleBarListener
import com.starblink.basic.style.title.TitleBar
import com.starblink.basic.util.KeyboardUtils
import com.starblink.wishlist.databinding.*
import com.starblink.wishlist.detailsadd.ui.cell.EmptyWishListAddCell
import com.starblink.wishlist.detailsadd.ui.cell.WishlistAddCell

@Route(path = RoutePage.Wishlist.DETAILS_ADD)
class DetailsAddActivity : BaseTVMActivity<ActivityDetailsAddBinding, DetailsAddVM>() {

    @Autowired(name = NavParamsKeyConst.WISHLIST_ID)
    @JvmField
    var wishId: String? = null //H5外链携带过来的userId app内传参也会传过来


    override fun initViewBinding(): ActivityDetailsAddBinding =
        ActivityDetailsAddBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewBinding.titleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                finish()
            }
        })
        refreshData()
    }

    private fun refreshData() {
        viewModel.initSomeData(wishId!!)
        viewBinding.rv.bindProductFeeds(
            allItemsAdapter, horGap = 0,
            spanCount = 3,
            verGap = 8
        ) as StaggeredGridLayoutManager?
        viewBinding.rv.itemAnimator = FadeInAnimator()
        viewBinding.rv.isNestedScrollingEnabled = true

//        viewBinding.rv.isNestedScrollingEnabled = false
//        viewBinding.rv.adapter = allItemsAdapter
//        viewBinding.rv.layoutManager = GridLayoutManager(mContext, 3)
//        viewBinding.rv.addItemDecoration(
//            SimpleGapItemDecor(
//                withEdge = true,
////                horGap = 8.dp2px(),
//                verGap = 8.dp2px()
//            )
//        )


        //刷新 不绑定state 单独处理
        viewBinding.state.refreshing()
        viewBinding.page.onMutRefresh {
            viewModel.refreshData(hasFinish = {
                viewBinding.page.finishRefresh()
                viewBinding.state.showContent()
            })
        }.refresh()
        viewBinding.page.onMutLoadMore {
            viewModel.loadMoreData(
                hasFinish = {
                    viewBinding.page.finishLoadMore()
                })
        }

        //提交
        viewBinding.btnCreate.click {
            if (canConfirm) {
                viewModel.move2Wishlist{
                    finish()
                }
            }
        }
        //搜索
        viewBinding.editSearchEditTxt.editText.imeOptions = EditorInfo.IME_ACTION_SEARCH
        viewBinding.editSearchEditTxt.editText.setOnEditorActionListener { _, actionId, _ ->
            if (EditorInfo.IME_ACTION_SEARCH == actionId) {
                doSearchAction(viewBinding.editSearchEditTxt.editText.text)
                true
            } else {
                false
            }
        }
        viewBinding.editSearchImg.click {
            doSearchAction(viewBinding.editSearchEditTxt.editText.text)
        }
        viewBinding.editSearchEditTxt.editText.addTextChangedListenerClosure {
            if (it.isBlank()) {
                doSearchAction("")
            }
        }

        viewModel.mAllItemsObserver.observe(this) {
            allItemsAdapter.notifyDataSetChanged()
        }
    }


    private fun doSearchAction(searchContent: CharSequence?) {
        if (!searchContent.isNullOrEmpty()) {
            val keyword = searchContent.toString()
            viewModel.curKeyword = keyword
            //设置搜索关键字 左匹配
        } else {
            viewModel.curKeyword = ""
        }
        KeyboardUtils.hideSoftkeyboard(mActivity)
        viewModel.refreshData(showDialog = true, hasFinish = {
            viewBinding.page.finishRefresh()
            viewBinding.state.showContent()
        })
    }


    private val allItemsAdapter: MutableAdapter by lazyOnNone {
        MutableAdapter(viewModel.mAllItemsObserver.value!!).apply {
            addVhDelegate {
                EmptyWishListAddCell(
                    EmptyViewWishFilterBinding.inflate(it.inflater, it, false)
                )
            }

            addVhDelegate {
                WishlistAddCell(
                    ItemDetailsAddBinding.inflate(it.inflater, it, false)
                ) {
                    handleUI()
                }
            }
        }
    }

    var canConfirm = false
    private fun handleUI() {
        if (viewModel.hasChoice()) {
            canConfirm = true
            viewBinding.btnCreate.setBackgroundResource(com.starblink.basic.style.R.color.sk_red600)
        } else {
            canConfirm = false
            viewBinding.btnCreate.setBackgroundResource(com.starblink.basic.style.R.color.sk_red600_opacity50)
        }
    }
}